package com.hk.service.product.impl;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.hk.commons.HqlFilter;
import com.hk.commons.entity.product.ProductInfo;
import com.hk.commons.entity.product.ProductParInfo;
import com.hk.dao.base.impl.BaseServiceImpl;
import com.hk.service.product.IProductInfo;
import com.hk.service.product.IProductParInfo;

@Service
public class ProductParInfoImpl extends BaseServiceImpl<ProductParInfo>
		implements IProductParInfo {

	@Resource
	private JdbcTemplate jdbcTemplate;

	@Autowired
	public IProductInfo prodService;

	@Override
	public ProductParInfo findById(Integer id) {
		
		ProductParInfo result = this.getById(id);
		
		if(result == null ){
			return null;
		}
		try {
			HqlFilter hqlFilter = new HqlFilter();
			hqlFilter.addEQFilter("parId", id);
			List<ProductInfo> prodList = prodService.findByFilter(hqlFilter);
			if (prodList != null && prodList.size() > 0) {
				ProductInfo prod = prodList.get(0);
				if(getFinancingAccount(id) != null){
					result.setFinancingAccount(getFinancingAccount(id));
				}
				
				result.setYraiseAccount(getYraiseAccount(id));
				result.setSraiseAccount(getSraiseAccount(id));
				result.setRiskLevel(prod.getRiskLevel());
				if (prod.getIntroduce() != null) {
					String introduceTxt;
					introduceTxt = new String(prod.getIntroduce(), "UTF-8");
					result.setProDesc(introduceTxt);
				}
				
				result.setMaxYearRate(getMaxYearRate(id));
				result.setMinYearRate(getMinYearRate(id));

			}

		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public void add(ProductParInfo u) {

		this.add(u);
	}

	@Override
	public List<ProductParInfo> getAll() {

		List<ProductParInfo> proParList = new ArrayList<ProductParInfo>();
		
		List<ProductParInfo> returnList = new ArrayList<ProductParInfo>();
		HqlFilter hqlFilter = new HqlFilter();
		proParList = this.findByFilter(hqlFilter);
		for(ProductParInfo parInfo:proParList){
			ProductParInfo newParInfo = findById(parInfo.getId());
			returnList.add(newParInfo);
		}

		return returnList;
	}

	private BigDecimal getFinancingAccount(Integer parId) {
		String sql = "SELECT sum(financingAccount)  FROM t_product_info where parId = "
				+ parId ;

		BigDecimal result = jdbcTemplate.queryForObject(sql, BigDecimal.class);

		return result == null ? new BigDecimal("0.00") : result;
	}

	private BigDecimal getYraiseAccount(Integer parId) {
		String sql = "SELECT sum(yraiseAccount)  FROM t_product_info where parId = "
				+ parId ;

		BigDecimal result = jdbcTemplate.queryForObject(sql, BigDecimal.class);

		return result == null ? new BigDecimal("0.00") : result;
	}

	private BigDecimal getSraiseAccount(Integer parId) {
		String sql = "SELECT sum(sraiseAccount)  FROM t_product_info where parId = "
				+ parId ;

		BigDecimal result = jdbcTemplate.queryForObject(sql, BigDecimal.class);

		return result == null ? new BigDecimal("0.00") : result;
	}
	
	private String getMinYearRate(Integer parId){
		String sql = "SELECT min(minYearRate)  FROM t_product_info where parId = "
				+ parId ;

		String result = jdbcTemplate.queryForObject(sql, String.class);

		return  result;
	}
	
	private String getMaxYearRate(Integer parId){
		String sql = "SELECT max(maxYearRate)  FROM t_product_info where parId = "
				+ parId ;

		String result = jdbcTemplate.queryForObject(sql, String.class);

		return  result;
	}
	  

}
